﻿using System;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Mvc;
using BoneSystem.Extensions.Enum;

namespace BoneSystem.Extensions.Annotations
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class InputAttribute : Attribute
    {
        private int _colSpan;

        public InputAttribute()
        {
            ColSpan = 1;
            Required = false;
            Class = "form-control";
            Margin = 10;
            InputLarge = InputLargeEnum.Normal;
        }
        public bool Required { get; set; }
        public string RequiredErrorMessage { get; set; }
        public string Change { get; set; }
        public string PlaceHolder { get; set; }
        public string Display { get; set; }
        public string Class { get; set; }
        public InputLargeEnum InputLarge { get; set; }
        public int Margin { get; set; }
        public int ColSpan
        {
            get
            {
                if (_colSpan <= 0)
                {
                    _colSpan = 1;
                }
                return _colSpan;
            }
            set { _colSpan = value; }
        }

        internal string Name { get; set; }
        internal string Id { get; set; }
        internal string NgModel { get; set; }
        public object Value { get; set; }

        public virtual TagBuilder TagBuilder()
        {
            var ret = new AngularTag("input");

            if (!string.IsNullOrEmpty(NgModel))
            {
                ret.Attributes["ng-model"] = NgModel;
            }
            if (!string.IsNullOrEmpty(Id))
            {
                ret.Attributes["id"] = Id;
            }
            if (!string.IsNullOrEmpty(Name))
            {
                ret.Attributes["name"] = Name;
            }
            if (!string.IsNullOrEmpty(Change))
            {
                ret.AddDirective(Change);
            }
            if (!string.IsNullOrEmpty(PlaceHolder))
            {
                ret.Attributes["placeholder"] = PlaceHolder;
            }

            

            if (Value != null)
            {
                ret.Attributes["value"] = Value.ToString();
            }

            ret.Attributes["style"] = "margin: " + Margin.ToString(CultureInfo.InvariantCulture) + "px";

            if (Required)
            {
                if (string.IsNullOrEmpty(RequiredErrorMessage))
                {
                    ret.AddDirective("required");
                }
                else
                {
                    ret.Attributes["required"] = RequiredErrorMessage;
                }
            }

            ret.Attributes["class"] =GetInputLarge();

            return ret;
        }

        protected string GetInputLarge()
        {
            var ret = "";
            switch (InputLarge)
            {
                case InputLargeEnum.Minimun:
                    ret= Class + " input-minimal";
                    break;
                case InputLargeEnum.Normal:
                    ret = Class + " input-normal";
                    break;
                case InputLargeEnum.Large:
                    ret = Class + " input-large";
                    break;
                case InputLargeEnum.XLarge:
                    ret = Class + " input-xlarge";
                    break;
            }
            return ret;
        }

        public virtual StringBuilder GetScripts()
        {
            return new StringBuilder("");
        }

        public virtual StringBuilder GetEvents()
        {
            return new StringBuilder();
        }
        public MvcHtmlString ToMvcHtmlString()
        {
            var tagTable = new TagBuilder("table");
            var tagRowLabel = new TagBuilder("tr");
            var tagRowInput = new TagBuilder("tr");
            var tagCollLabel = new TagBuilder("td");
            var tagCollInput = new TagBuilder("td");

            var tagLabel = new TagBuilder("label");
            tagLabel.Attributes["for"] = Id;
            tagLabel.Attributes["class"] = "label-form";
            tagLabel.InnerHtml = Display;

            //tagCollInput.Attributes["width"] = Width.ToString(CultureInfo.InvariantCulture);
            // Passa o conteúdo para as colunas
            tagCollLabel.InnerHtml = new MvcHtmlString(tagLabel.ToString(TagRenderMode.Normal)).ToHtmlString();
            tagCollInput.InnerHtml = new MvcHtmlString(TagBuilder().ToString(TagRenderMode.Normal)).ToHtmlString();

            // Adiciona as colunas à linha
            tagRowLabel.InnerHtml = new MvcHtmlString(tagCollLabel.ToString(TagRenderMode.Normal)).ToHtmlString();
            tagRowInput.InnerHtml += new MvcHtmlString(tagCollInput.ToString(TagRenderMode.Normal)).ToHtmlString();

            // Aciciona à linha na tabela
            tagTable.InnerHtml = new MvcHtmlString(tagRowLabel.ToString(TagRenderMode.Normal)).ToHtmlString();
            tagTable.InnerHtml += new MvcHtmlString(tagRowInput.ToString(TagRenderMode.Normal)).ToHtmlString();

            return new MvcHtmlString(tagTable.ToString(TagRenderMode.Normal));
        }
    }
}
